<?php
header("Access-Control-Allow-Origin: *");
// !!!!!!!!!! timezone !!!!!!!!!!!!
date_default_timezone_set('Asia/Shanghai');

use Silex\Application;

use Silex\Provider\AssetServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Silex\Provider\HttpCacheServiceProvider;
use Symfony\Component\HttpFoundation\Request;

$app = new Application();
$app->register(new ServiceControllerServiceProvider());
// assets
$app->register(new Silex\Provider\AssetServiceProvider(), array(
	'assets.version' => C_VERSION,
    'assets.version_format' => '%s?v=%s',
    'assets.named_packages' => array(
        // 'images' => array('base_urls' => array('https://img.example.com')),
    ),
));

// http cache
$app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
   'http_cache.cache_dir' => __DIR__.'/../cache/'
));
Request::setTrustedProxies(array('127.0.0.1'));

$app->register(new TwigServiceProvider());
$app->register(new HttpFragmentServiceProvider());
$app['twig'] = $app->extend('twig', function ($twig, $app) {
    // add custom globals, filters, tags, ...
    return $twig;
});

// mysql
if (C_DEBUG) {
    $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
        'db.options' => array(
            'driver'   => 'pdo_mysql',
            'host'      => '127.0.0.1',
            'dbname'    => 'pt_public_market',
            'port'      => 3306,
            'user'      => 'root',
            'password'  => 'jbx666',
            'charset'   => 'utf8mb4'
        )
    ));
} else {
    $app->register(new Silex\Provider\DoctrineServiceProvider(), array(
        'db.options' => array(
            'driver'   => 'pdo_mysql',
            'host'      => '127.0.0.1',
            'dbname'    => 'pt_public_market',
            'port'      => 3306,
            'user'      => 'root',
            'password'  => 'jbx666',
            'charset'   => 'utf8mb4'
        )
    ));
}

/************** auto load class ******************/
function autoLoadClass( $class ) {
	$file =  $class . '.class.php';
	$file  = str_replace('\\', DIRECTORY_SEPARATOR, $file);
	if (is_file('../'. $file)) {
		require_once(__DIR__ . '/../' . $file);
	}
}
spl_autoload_register('autoLoadClass');

return $app;
